package cn.gok.server.controller;

import cn.gok.common.config.SystemLog;
import cn.gok.common.entity.backstage.Picture;
import cn.gok.common.resp.PageResult;
import cn.gok.common.resp.Result;
import cn.gok.server.service.PictureService;
import cn.gok.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: DxlinY
 * @apiNote: 图片管理控制层
 * @date: 2020/10/10
 * @time: 11:02
 */
@RestController
@RequestMapping("picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /**
     * 文件集、
     */
    private MultipartFile[] catchFiles;

    /**
     * 文件上传到缓存，备用
     *
     * @param multipartFiles 文件集
     */
    @PostMapping("uploadImageCatch")
    @SystemLog(model = "图片管理", method = "上传图片到缓存")
    public Result uploadImage(MultipartFile[] multipartFiles) {
        if (multipartFiles.length > 0) {
            try {
                for (MultipartFile file : multipartFiles) {
                    if (file.getInputStream().available() > 1024000) {
                        return ResultUtil.imageTooBig();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            catchFiles = multipartFiles;
        }
        return ResultUtil.success(null);
    }

    /**
     * 删除图片缓存备份
     */
    @GetMapping("delImageCatch")
    @SystemLog(model = "图片管理", method = "删除图片缓存")
    public void delImageCatch() {
        catchFiles = null;
    }

    /**
     * 新增一个图片对象
     *
     * @param picture 图片对象
     * @return result
     */
    @PostMapping("insertPicture")
    @SystemLog(model = "图片管理", method = "添加一个图片对象")
    public Result addPicture(Picture picture) {
        if (pictureService.insertPictures(picture, catchFiles) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 查询列表
     *
     * @param page  页数
     * @param limit 页容量
     * @return result
     */
    @PostMapping("listPicture")
    @SystemLog(model = "图片管理", method = "图片分页接口")
    public PageResult listPicture(int page, int limit, Picture picture) {
        return ResultUtil.pageInfo(pictureService.listAll(page, limit, picture));
    }

    /**
     * 通过id编号修改图片的启用状态
     *
     * @param pictureId 图片id
     * @param status    启用状态
     * @return result
     */
    @PostMapping("updateStatusById")
    @SystemLog(model = "图片管理", method = "通过id修改图片启用状态")
    public Result updateStatusById(long pictureId, boolean status) {
        if (pictureService.updateStatusById(pictureId, status) > 0) {
            return ResultUtil.success(status);
        }
        return ResultUtil.error(null);
    }
}
